package net.begincode.service.course.impl;

import java.util.List;

import utils.CacheUtils;
import config.DbTemplate;
import net.begincode.bean.BcCourse;
import net.begincode.bean.CourseChapter;
import net.begincode.bean.ProjectStage;
import net.begincode.service.course.CourseService;
import net.begincode.service.user.impl.BcUserServiceImpl;

public class CourseServiceImpl implements CourseService {
	private CourseServiceImpl() {
	}

	private static CourseServiceImpl instance = null;

	public static CourseServiceImpl getInstance() {
		if (instance == null) {
			synchronized (CourseServiceImpl.class) {
				if (instance == null) {
					instance = new CourseServiceImpl();
				}
			}
		}
		return instance;
	}
	@Override
	public List<BcCourse> selNewCourseLimitN(int n) {
		// TODO Auto-generated method stub
		String selSql = " select * from bc_course order by recommend limit ? ";
		return DbTemplate.queryForList(selSql, new Object[]{n},BcCourse.class);
	}

	@Override
	public List<BcCourse> selAllCourse() {
		// TODO Auto-generated method stub
		String selSql =" select * from bc_course ";
		return DbTemplate.queryForListNoParam(selSql, BcCourse.class);
	}

	@Override
	public int addCourse(BcCourse bcCourse) {
		// TODO Auto-generated method stub
		StringBuilder addSql = new StringBuilder();
		addSql.append(" insert into bc_course (course_name ,keynote ,introduction ,bc_navigation_id ,recommend ,bc_user_id ,is_begincode ,check_flag ) ");
		addSql.append(" values (?,?,?,?,?,?,?,?)  ");
		return DbTemplate.insert(addSql.toString(), new Object[]{bcCourse.getCourse_name(),bcCourse.getKeynote(),bcCourse.getIntroduction(),bcCourse.getBc_navigation_id()
			,bcCourse.getRecommend(),bcCourse.getBc_user_id(),bcCourse.getIs_begincode(),bcCourse.getCheck_flag()});
	}
	@Override
	public BcCourse selCourseById(String courseId) {
		// TODO Auto-generated method stub
		String selSql = " select * from bc_course where bc_course_id = ? ";
		return DbTemplate.queryForObject(selSql, new Object[]{courseId}, BcCourse.class);
	}
	@Override
	public int updCourse(BcCourse bcCourse) {
		// TODO Auto-generated method stub
		StringBuilder updSql = new StringBuilder();
		updSql.append(" update bc_course set course_name = ? ");
		updSql.append(" ,bc_navigation_id = ? ");
		updSql.append(" ,keynote = ?  ");
		updSql.append(" ,introduction = ? ");
		updSql.append(" where bc_course_id = ? ");
		return DbTemplate.update(updSql.toString(), new Object[]{bcCourse.getCourse_name(),bcCourse.getBc_navigation_id()
			,bcCourse.getKeynote(),bcCourse.getIntroduction(),bcCourse.getBc_course_id()});
	}
	@Override
	public BcCourse selCourseByIdCache(String courseId) {
		// TODO Auto-generated method stub
		String selSql = " select * from bc_course where bc_course_id = ? ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.COURSE_CACHE,  "courseId"+courseId);
		if(dbEle != null){
			return (BcCourse)dbEle;
		}else{
			BcCourse ccs = DbTemplate.queryForObject(selSql, new Object[]{courseId}, BcCourse.class);
			CacheUtils.addEleByParam(CacheUtils.COURSE_CACHE, "courseId"+courseId, ccs);
			return ccs;
		}
	}

}
